﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using EFCoreRepository.Repositories;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ.Models;
using Zoomtel.Persist.Query;

namespace Zoomtel.Persist.BIZ
{
    public class CashLogRepository : RepositoryAb<CashLogEntity>
    {
        public CashLogRepository(DefaultDbContext context) : base(context)
        {
        }

        public async Task<QueryResultModel<CashLogModel>> Query(CashLogQueryModel model)
        {
            var paging = model.Paging();
            var agentInfos = DbContext.Set<AgentInfoEntity>();
            //var query = dbSet.AsQueryable();
            var query = from item in dbSet
                    join agent in agentInfos on item.Uid equals agent.Id
                    select new CashLogModel
                    {
                        Id = item.Id,
                        Uid = agent.Id,
                        UserCode = agent.AgentCode,
                        UserName = agent.AgentName,
                        Money = item.Money,
                        Createtime = item.Createtime,
                        PassTime = item.PassTime,
                        BankInfoId = item.BankInfoId,
                        Status = item.Status,
                        StatusName = item.Status == "0" ? "审核中" : item.Status == "10" ? "审核通过" : item.Status == "20" ? "审核驳回" : "已打款",
                        CardNo = item.CardNo,
                        Remark = item.Remark,
                        TradeNo = item.TradeNo,
                        Transfer_Money = item.Transfer_Money,
                        Transfer_Fee = item.Transfer_Fee,
                        Fee_Rate = item.Fee_Rate
                    };
            if (model.Status != null && !"".Equals(model.Status) && !"-1".Equals(model.Status))
            {
                query = query.Where(a => a.Status == model.Status);
            }
            if (model.AgentInfo != null && !"".Equals(model.AgentInfo))
            {
                query = query.Where(a => a.UserCode.Contains(model.AgentInfo) || a.UserName.Contains(model.AgentInfo));
            }
            if (model.AgentId != null && !"".Equals(model.AgentId))
            {
                query = query.Where(a => a.Uid == model.AgentId);
            }
            if (model.Id != null && !"".Equals(model.Id))
            {
                query = query.Where(a => a.Id == model.Id);
            }
            if (!paging.OrderBy.Any())
            {
                query = query.OrderByDescending(m => m.Createtime);
            }
            return await query.PaginationGetResult(paging);
        }
    }
}
